package com.ruoyi.web.controller.substation;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.system.domain.SDeviceProtectDevice;
import com.ruoyi.system.domain.SSubstationDevice;
import com.ruoyi.system.service.ISDeviceProtectDeviceService;
import com.ruoyi.system.service.ISSubstationDeviceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SProtectDevice;
import com.ruoyi.system.service.ISProtectDeviceService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 保护装置信息Controller
 * 
 * @author ruoyi
 * @date 2023-07-05
 */
@Api(tags = "保护装置信息管理")
@RestController
@RequestMapping("/substation/protectDevice")
public class SProtectDeviceController extends BaseController
{
    @Autowired
    private ISProtectDeviceService sProtectDeviceService;
    @Autowired
    private ISDeviceProtectDeviceService deviceProtectDeviceService;

    /**
     * 查询保护装置信息列表
     */
    @ApiOperation("获取保护装置信息列表")
    @PreAuthorize("@ss.hasPermi('substation:protectDevice:list')")
    @GetMapping("/list")
    public TableDataInfo list(SProtectDevice sProtectDevice)
    {
        startPage();
        List<SProtectDevice> list = sProtectDeviceService.list(sProtectDevice.queryWrapper());
        return getDataTable(list);
    }

    /**
     * 导出保护装置信息列表
     */
    @ApiOperation("导出保护装置信息列表")
    @PreAuthorize("@ss.hasPermi('substation:protectDevice:export')")
    @Log(title = "保护装置信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SProtectDevice sProtectDevice)
    {
        List<SProtectDevice> list = sProtectDeviceService.list(sProtectDevice.queryWrapper());
        ExcelUtil<SProtectDevice> util = new ExcelUtil<SProtectDevice>(SProtectDevice.class);
        util.exportExcel(response, list, "保护装置信息数据");
    }

    /**
     * 导入保护装置信息
     */
    @ApiOperation("导入保护装置信息")
    @PreAuthorize("@ss.hasPermi('substation:protectDevice:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        String message = "操作成功";
        try {
            message = sProtectDeviceService.importData(file);
        }catch (IllegalStateException e){
            throw new ServiceException("请检查表格格式是否为文本格式!");
        }
        return success(message);
    }

    /**
     * 获取保护装置信息详细信息
     */
    @ApiOperation("获取保护装置信息详细")
    @ApiImplicitParam(name = "id", value = "保护装置信息ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('substation:protectDevice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sProtectDeviceService.getById(id));
    }

    /**
     * 新增保护装置信息
     */
    @ApiOperation("新增保护装置信息")
    @PreAuthorize("@ss.hasPermi('substation:protectDevice:add')")
    @Log(title = "保护装置信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SProtectDevice sProtectDevice)
    {
        sProtectDeviceService.checkProtectDevice(sProtectDevice);
        return toAjax(sProtectDeviceService.save(sProtectDevice));
    }

    /**
     * 修改保护装置信息
     */
    @ApiOperation("更新保护装置信息")
    @PreAuthorize("@ss.hasPermi('substation:protectDevice:edit')")
    @Log(title = "保护装置信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SProtectDevice sProtectDevice)
    {
        SProtectDevice byId = sProtectDeviceService.getById(sProtectDevice.getId());
        if(!sProtectDevice.getProtectDeviceModel().equals(byId.getProtectDeviceModel())) {
            sProtectDeviceService.checkProtectDevice(sProtectDevice);
        }
        return toAjax(sProtectDeviceService.updateById(sProtectDevice));
    }

    /**
     * 删除保护装置信息
     */
    @ApiOperation("删除保护装置信息")
    @ApiImplicitParam(name = "ids", value = "保护装置信息ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('substation:protectDevice:remove')")
    @Log(title = "保护装置信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        for (String id : ids) {
            long count = deviceProtectDeviceService.count(new LambdaQueryWrapper<SDeviceProtectDevice>()
                    .eq(SDeviceProtectDevice::getProtectDeviceId, id));
            if(count > 0){
                throw new ServiceException("保护装置已绑定设备！");
            }
        }
        return toAjax(sProtectDeviceService.removeByIds(ids));
    }
}
